<extend name="Public:base" />
<block name="javascript">
<script src="__PUBLIC__/ztree/jquery.ztree.all-3.5.min.js"></script>
<link  rel="stylesheet" type="text/css" href="__PUBLIC__/ztree/zTreeStyle.css" />
<link  rel="stylesheet" type="text/css" href="__PUBLIC__/ztree/demo.css" />


	<script src="__PUBLIC__/fileupload/jquery.ui.widget.js"></script>
	<script src="__PUBLIC__/fileupload/jquery.fileupload.js"></script>
<script>
$(function(){

	
	$('.J_SubmitPL').click(function(e){    	
    	//$('#alert').css({'display':'none'});
    	$("#dialog").css('display','none');
		$('.ks-ext-mask').css('display','none');
		
    	var zTree = $.fn.zTree.getZTreeObj("category_tree"),
		
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			
			if(treeNode!=undefined){
				var isp= nodes[0].isParent;
			}else{
				var isp= true;
			}
			
			
			var id=(treeNode==undefined?0:treeNode.id);

			var url;
			
			if($("input[name='action']").val()=='add'){
				url='{:U("GoodsBrand/add")}';
			}else if($("input[name='action']").val()=='edit'){
				url='{:U("GoodsBrand/edit")}';
			}
			/* 添加数据要填写的字段 */
			$.post(
				url,
				{					
					'id':id,
					'name':$("input[name='name']").val(),
					'image':$("input[name='image']").val(),
					'goods_class_id':$(".category").val(),
					'brand_logo':$("input[name='brand_logo']").val(),
					'brand_description':$("textarea[name='brand_description']").val(),
					'recommend':$("input[name='recommend']:checked").val(),
					'letter':$("input[name='letter']").val(),
					'brand_banner':$("input[name='brand_banner']").val(),
					'sort_order':$("input[name='sort_order']").val(),
				},
				function(d){			
					if($("input[name='action']").val()=='add'){						
											
						//有父节点
				    	if(treeNode){
				    		treeNode = zTree.addNodes(treeNode, {id:d.id, pId:id, isParent:isp, name:d.name});
				    	}else{
				    		treeNode = zTree.addNodes(treeNode, {id:d.id, pId:0, isParent:isp, name:d.name});
				    	}	
					}else{
						
						if(d.success){
							nodes[0].name = d.name;
							zTree.updateNode(nodes[0]);
						}
						if(d.err){
							alert(d.err);
						}
						
						
					}
					
				}
			);
    	
    });



	// Image Manager
	$(document).delegate('a[data-toggle=\'image\']', 'click', function(e) {
		e.preventDefault();

		var element = this;

		$(element).popover({
			html: true,
			placement: 'right',
			trigger: 'manual',
			content: function() {
				return '<button type="button" id="button-image" class="btn btn-primary"><i class="icon-edit"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="icon-trash"></i></button>';
			}
		});

		$(element).popover('toggle');

		$('#button-image').on('click', function() {
			$('#modal-image').remove();

			$('#form-upload').remove();

			$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_image" type="file" name="file" /></form>');

			$('#form-upload input[name=\'file\']').trigger('click');

			$(element).popover('hide');

			$('[osctype="btn_upload_image"]').fileupload({

				dataType: 'json',
				url: "{:U('Image/upload_image',array('dir'=>'goods_brand'))}",
				add: function(e, data) {
					$parent = $('#thumb');
					$input = $parent.find('[osctype="image_input"]');
					$img = $parent.find('[osctype="image"]');
					data.formData = {old_product_image:$input.val()};
					$img.attr('src', "__IMG__/loading.gif");
					data.submit();
				},
				done: function (e,data) {

					var image=data.result;


					$parent = $('#thumb');
					$input = $parent.find('[osctype="image_input"]');
					$img = $parent.find('[osctype="image"]');
					if(image) {
						// $img.prev('i').hide();
						$img.attr('src', '__ROOT__'+image.image_thumb);
						$img.show();
						$input.val(image.image);
					} else {
						alert('上传失败');
					}
				}
			});



		});

		$('#button-clear').on('click', function() {
			$(element).find('img').attr('src', $(element).find('img').attr('data-placeholder'));

			$(element).parent().find('input').attr('value', '');

			$(element).popover('hide');
		});
	});



	
});
</script>

<script>

		var setting = {
			view: {
				addHoverDom: false,
				removeHoverDom: false,
				selectedMulti: false
			},
			edit: {
				enable: true,
				editNameSelectAll: true,
				showRemoveBtn: false,
				showRenameBtn: false
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};

	
	var zNodes ={$list};

	function add(e) {

		$("input[name='action']").val('add');
		$('.title').find('span').text('新增分类');	
		$("#dialog").css({'position' : 'fixed','display' : 'block', 'z-index' : '9999'});
		
	}
	function edit() {
			$("input[name='action']").val('edit');

			var zTree = $.fn.zTree.getZTreeObj("category_tree"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			if (nodes.length == 0) {
				alert("请先选择一个节点");
				return;
			}else{
				var id=treeNode.id;
				$.post(
				'{:U("GoodsBrand/get_info")}',
				{					
					'id':id,
					
				},
				function(d){
					$('img').attr('src', d.image);
					$("input[name='name']").val(d.name);
					$(".category").find('option:selected').html(d.cname);
					$("input[name='goods_class_id']").val(d.goods_class_id);
					$("input[name='letter']").val(d.letter);
					//$("input[name='recommend']").val(d.recommend);
					if(d.recommend == 1){
						$("input[class='name1']").attr('checked','checked');
					}else {
						$("input[class='name2']").attr('checked','checked');
					}
					$("textarea[name='brand_description']").val(d.brand_description);
					$("input[name='sort_order']").val(d.sort_order);
				}
			);
				$('.title').find('span').text('编辑分类');	
				$("#dialog").css({'position' : 'fixed','display' : 'block', 'z-index' : '9999'});
				
				
				
			}
	}
	function remove(e) {
		if(!confirm('确认要删除吗！！')){
				return false;
			}
			
			var zTree = $.fn.zTree.getZTreeObj("category_tree"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			if (nodes.length == 0) {
				alert("请先选择一个节点");
				return;
			}
			$.post(
				'{:U("GoodsBrand/del")}',
				{					
					'id':treeNode.id,					
				},
				function(d){			
					if(d.err){
						alert(d.err);
					}else{						
						zTree.removeNode(treeNode);
					}
					
					
				}
			);
	}
	
		$(document).ready(function(){
			$.fn.zTree.init($("#category_tree"), setting, zNodes);
			$("#addParent").bind("click", {isParent:true}, add);
			$("#edit").bind("click", edit);
			$("#remove").bind("click", remove);
			
			//关闭弹出层
			$('#dialog').on('click','.ks-ext-close',function(){	
			    $("#dialog").css('display','none');
			    $('.ks-ext-mask').css('display','none');
			    return false;
			});
			//关闭弹出层
			$('#dialog').on('click','.J_Cancel',function(){	
			    $("#dialog").css('display','none');
			    $('.ks-ext-mask').css('display','none');
			    return false;
			});
			
			
		});
	
</script>
</block>

<block name="content">
	<link rel="stylesheet" href="__CSS__/dialog.css" />	
	
	
	<div class="ks-ext-mask" style="position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 999; display:none"></div>
	<div id="dialog" class="dialog" style="z-index: 9999; display:none">
    <div class="ks-contentbox" style="OVERFLOW-Y: scroll; OVERFLOW-X:hidden; width: 640px; height: 410px">
      <div class="title"><span>新增品牌</span><a class="ks-ext-close" href="javascript:void(0)">X</a></div>
      <input type="hidden" name="action" value="" />	


        <dl>
			<!--图片-->
			<dt>品牌 logo</dt>
			<dd style="padding-left: 0px;" id="thumb">
				<a href="#" id="thumb-image" data-toggle="image" class="img-thumbnail">
					<img osctype="image" <if condition="isset($slider['image'])">
					src="__ROOT__{$thumb_image}"
					<else />
					src="__ROOT__/Common/image/no_image_100x100.jpg"
				</if>  />
				</a>
				<input osctype="image_input" type="hidden" name="image" value="<?php echo $slider['image']; ?>" id="input-image" />
			</dd>
			<!--图片-->

        	<dt>品牌名称</dt>
        	<dd><input type="text" name="name" class="text w400" /></dd>
        	<dt>所属分类</dt>
        	<dd>
				<select id="input-stock-status" class="w400 category" name="goods_class_id">
					<option value="">请选择分类</option>
					<volist name="category" id="v">
						<option value="{$v.id}">{$v.name}</option>
					</volist>
				</select>
			</dd>
			<dt>品牌字母</dt>
			<dd><input type="text" name="letter" class="text w400" placeholder="例如 A" /></dd>
			<dt>是否热门</dt>
			<dd>是:<input type="radio" value="1" name="recommend" class="name1" /> 否:<input type="radio" value="0" name="recommend" class="name2" /></dd>
        	<dt>品牌描述</dt>
        	<dd><textarea name="brand_description" class="text h50 w400"></textarea></dd>
        	<dt>排序</dt>
        	<dd><input type="text" name="sort_order" class="text" /></dd>
        </dl>


        <div class="J_DefaultMessage"></div>
        <div class="bottom">
        	 <a href="javascript:void(0);" class="J_SubmitPL ncsc-btn ncsc-btn-green">确认</a> 
        	 <a href="javascript:void(0);" class="J_Cancel ncsc-btn">取消</a> </div>
      
    </div>
  </div>  
	

	<div class="page-header">
	<a id="addParent" class="btn btn-primary">新增</a>
	<a id="edit" class="btn btn-primary">编辑</a>
	<a id="remove" class="btn btn-primary">删除</a>
	</div>
	
	<div class="row">
		<div id="category_tree" class="ztree"></div>
	</div>
	
</block>

